package com.matt.service.impl;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.domain.Feeds;
import com.matt.foundation.model.ErrorCode;
import com.matt.repository.FeedsRepository;
import com.matt.service.FeedsService;
import com.matt.service.TeacherService;
import com.matt.service.remoteCall.SsoCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/11/29
 * *******************************
 */
@Service
public class FeedsServiceImpl implements FeedsService{

    @Autowired
    private FeedsRepository feedsRepository;

    @Autowired
    TeacherService teacherService;

    @Autowired
    SsoCallService ssoCallService;

    @Transactional
    @Override
    public Feeds save(String staffId, String childrenId, String areaId, String event, String eventType, String title, String content, String images, String videos, String linkId) {

        StaffDTO teacherInfo = ssoCallService.getStaffDTO(null, staffId);

        Feeds f = Feeds.of(staffId, childrenId, areaId, event, eventType, title, content, images, videos, linkId,"");

        f.setTeacherName(teacherInfo.getRealName());
        f.setTeacherImg(teacherInfo.getHeadImgUrl());

        Feeds feeds = feedsRepository.saveAndFlush(f);
        return feeds;
    }

    @Override
    public Feeds findById(String ticket, String id) {
        Feeds feeds = feedsRepository.getOne(id);
        if(Objects.isNull(feeds))
            throw new BusinessException(ErrorCode.FEEDS_NOT_EXIST,"不存在！");
        return feeds;
    }
}

